home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / src / tclXcnvclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-19  |  4.3 KB  |  159 lines

  1. /* 
  2.  * tclXcnvclock.c --
  3.  *
  4.  *      Contains the TCL convertclock command.  This is in a module seperate
  5.  * from clock so that it can be excluded, along with the yacc generated code,
  6.  * since its rather large.
  7.  *-----------------------------------------------------------------------------
  8.  * Copyright 1991-1993 Karl Lehenbauer and Mark Diekhans.
  9.  *
  10.  * Permission to use, copy, modify, and distribute this software and its
  11.  * documentation for any purpose and without fee is hereby granted, provided
  12.  * that the above copyright notice appear in all copies.  Karl Lehenbauer and
  13.  * Mark Diekhans make no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without express or
  15.  * implied warranty.
  16.  *-----------------------------------------------------------------------------
  17.  * $Id: tclXcnvclock.c,v 3.0 1993/11/19 06:58:28 markd Rel $
  18.  *-----------------------------------------------------------------------------
  19.  */
  20.  
  21. #include "tclExtdInt.h"
  22.  
  23. int
  24. Tcl_GetTimeZone _ANSI_ARGS_((long  currentTime));
  25.  
  26.  
  27. /*
  28.  *-----------------------------------------------------------------------------
  29.  *
  30.  * Tcl_GetTimeZone --
  31.  *   Determines the current timezone.  The method varies wildly between
  32.  * different Unix implementations, so its hidden in this function.
  33.  *
  34.  * Parameters:
  35.  *   o currentTime (I) - The clock value that is to be used for the current
  36.  *     time.  This is really a time_t, but is long so it can be in tclExtend.h
  37.  *     without requiring time_t.
  38.  * 
  39.  * Returns:
  40.  *    Minutes east of GMT.
  41.  *-----------------------------------------------------------------------------
  42.  */
  43. int
  44. Tcl_GetTimeZone (currentTime)
  45.     long  currentTime;
  46. {
  47. #ifdef TCL_USE_TM_TZADJ
  48.     time_t      curTime = (time_t) currentTime;
  49.     struct tm  *timeDataPtr = localtime (&curTime);
  50.     int         timeZone;
  51.  
  52.     timeZone = timeDataPtr->tm_tzadj  / 60;
  53.     if (timeDataPtr->tm_isdst)
  54.         timeZone += 60;
  55.  
  56.     return timeZone;
  57. #endif
  58.  
  59. #ifdef TCL_USE_TM_GMTOFF
  60.     struct tm *timeDataPtr = localtime (¤tTime);
  61.     int        timeZone;
  62.  
  63.     timeZone = -(timeDataPtr->tm_gmtoff / 60);
  64.     if (timeDataPtr->tm_isdst)
  65.         timeZone += 60;
  66.  
  67.     return timeZone;
  68. #endif
  69.  
  70. #ifdef TCL_USE_TIMEZONE_VAR
  71.     static int setTZ = FALSE;
  72.     int        timeZone;
  73.  
  74.     if (!setTZ) {
  75.         tzset ();
  76.         setTZ = TRUE;
  77.     }
  78.     timeZone = timezone / 60;
  79.  
  80.     return timeZone;
  81. #endif
  82.  
  83. #ifdef TCL_USE_GETTIMEOFDAY
  84.     struct timeval  tv;
  85.     struct timezone tz;
  86.     int             timeZone;
  87.  
  88.     gettimeofday (&tv, &tz);
  89.     timeZone = tz.tz_minuteswest;
  90.  
  91.     return timeZone;
  92. #endif
  93.  
  94. #ifndef TCL_GOT_TIMEZONE
  95.    /*
  96.     * Cause compile error.  The defines are done in tclExtdInt.h based on
  97.     * what autoconf found.
  98.     */
  99.   error: autoconf did not figure out how to determine the timezone. 
  100. #endif
  101.  
  102. }
  103.  
  104. /*
  105.  *-----------------------------------------------------------------------------
  106.  *
  107.  * Tcl_ConvertclockCmd --
  108.  *     Implements the TCL convertclock command:
  109.  *         convertclock dateString ?GMT|{}?
  110.  *
  111.  * Results:
  112.  *     Standard TCL results.
  113.  *
  114.  *-----------------------------------------------------------------------------
  115.  */
  116. int
  117. Tcl_ConvertclockCmd (clientData, interp, argc, argv)
  118.     ClientData  clientData;
  119.     Tcl_Interp *interp;
  120.     int         argc;
  121.     char      **argv;
  122. {
  123.     long        clockVal;
  124.     time_t      baseClock;
  125.     long        zone;
  126.  
  127.     if ((argc < 2) || (argc > 4)) {
  128.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  129.                           " dateString ?GMT|{}? ?baseclock?", (char *) NULL);
  130.     return TCL_ERROR;
  131.     }
  132.     if (argc == 4) {
  133.         if (Tcl_GetTime (interp, argv [3], &baseClock) != TCL_OK)
  134.             return TCL_ERROR;
  135.     } else {
  136.         time (&baseClock);
  137.     }
  138.  
  139.     if ((argc > 2) && (argv [2][0] != '\0')) {
  140.         if (!STREQU (argv [2], "GMT")) {
  141.             Tcl_AppendResult (interp, "invalid argument: expected `GMT', ",
  142.                               "got : `", argv [2], "'", (char *) NULL);
  143.             return TCL_ERROR;
  144.         }
  145.         zone = -50000; /* Force GMT */
  146.     } else {
  147.         zone = Tcl_GetTimeZone (baseClock);
  148.     }
  149.  
  150.     if (Tcl_GetDate (argv [1], baseClock, zone, &clockVal) < 0) {
  151.         Tcl_AppendResult (interp, "Unable to convert date-time string \"",
  152.                           argv [1], "\"", (char *) NULL);
  153.     return TCL_ERROR;
  154.     }
  155.     sprintf (interp->result, "%ld", clockVal);
  156.     return TCL_OK;
  157. }
  158.  
  159.